﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using WatiN.Core;
using WatiN.Core.Native.Windows;
using WebAutomation.Browsers;

namespace WebAutomation
{
    public class BrowserCommand : CommandBase
    {
        public BrowserCommand(WebBrowserObject browserObject)
        {
            this.Initialize(browserObject);

        }

        public override bool ExecuteCommand(WebBrowserObject browserObject, string[] parameter, string url, string testRepo, string testResultLocation, string dbConnString, ref DataSet dataFromDB, ref TestLineResult testLineResult, ref bool stopTest, WebAutoUtils.ITestDataExceptionRules testDataExceptionRules)
        {
            string command = parameter[0];
            testLineResult.LineCommand = command;
            testLineResult.LineParameter = "";
            try
            {
                testLineResult.LineParameter = parameter[1];
            }catch
            {
                
            }
            if (this.BrowserObject == null)
            {
                this.BrowserObject = browserObject;
            }
            testLineResult.LineSubCommand = command;
            try
            {
                switch(command.ToLower())
                {
                    case "redirect":
                        string urlRedirect = parameter[1];
                        RedirectToURL(urlRedirect);
                        testLineResult.LinePassed = true;
                        break;
                    case "data":
                        testLineResult.LineCommand = "Open browser";
                        GoToURL(url);
                        testLineResult.LinePassed = true;
                        break;
                    case "refresh":
                        this.BrowserObject.Refresh("");
                        testLineResult.LinePassed = true;
                        break;

                }
               

            }catch(Exception ex)
            {
                testLineResult.IsFrameworkError = true;
                testLineResult.LineParameter = parameter[1];
                testLineResult.LineMessage = "Error." + ex.Message + "." + ex.StackTrace;
                testLineResult.LinePassed = false;
            }
            return testLineResult.LinePassed;
        }

        public virtual void RedirectToURL(string url)
        {
            this.BrowserObject.GoTo(url);

        }

        public virtual void GoToURL(string url)
        {
            this.BrowserObject.ShowWindow(NativeMethods.WindowShowStyle.ShowMaximized);
            this.BrowserObject.GoTo(url);
        }
    }
}
